package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"vehi-info-sys/cache"
	"vehi-info-sys/models"
)

func CarConAdd(c *gin.Context) {
	carConNo := c.PostForm("carConNo")
	carConName := c.PostForm("carConName")
	carConNum := c.PostForm("carConNum")

	if carConNo == "" || carConName == "" || carConNum == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}

	data := models.CarCon{
		CarConNo:   carConNo,
		CarConName: carConName,
		CarConNum:  carConNum,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func ConTractAdd(c *gin.Context) {
	conTractNo := c.PostForm("conTractNo")
	conName := c.PostForm("conName")
	conPrice, _ := strconv.Atoi(c.PostForm("conPrice"))
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	if conTractNo == "" || conName == "" || conPrice == 0 || cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Contract{
		ContractNo: conTractNo,
		ConName:    conName,
		ConPrice:   conPrice,
		Cid:        cid,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func CarConTractList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	/*
		SELECT
		  car_cons.id as CarConId,
		  contracts.contract_no as ContractNo,
		  contracts.con_name as ConName,
		  contracts.con_price as ConPrice,
		  car_cons.car_con_no as CarConNo,
		  car_cons.car_con_name as CarConName,
		  car_cons.car_con_num as CarConNum
		FROM
		  contracts
		  LEFT JOIN car_cons ON car_cons.id = contracts.cid
		WHERE
		  car_cons.id = 1
	*/

	var contracts []models.CarConTractList
	redis_key := "contracts_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("contracts").
			Select("car_cons.id as CarConId",
				"contracts.contract_no as ContractNo",
				"contracts.con_name as ConName",
				"contracts.con_price as ConPrice",
				"car_cons.car_con_no as CarConNo",
				"car_cons.car_con_name as CarConName",
				"car_cons.car_con_num as CarConNum").
			Joins("LEFT JOIN car_cons ON car_cons.id = contracts.cid").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&contracts)

		jsonData, _ := json.Marshal(contracts)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour)
	} else {
		json.Unmarshal([]byte(cacheData), &contracts)
	}
	var count int64
	models.DB.Model(&models.Contract{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{},
	})
	return
}

func CarConTractDetail(c *gin.Context) {
	id := c.Query("id")
	var contracts []models.CarConTractList
	redis_key := "contracts_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("contracts").
			Select("car_cons.id as CarConId",
				"contracts.contract_no as ContractNo",
				"contracts.con_name as ConName",
				"contracts.con_price as ConPrice",
				"car_cons.car_con_no as CarConNo",
				"car_cons.car_con_name as CarConName",
				"car_cons.car_con_num as CarConNum").
			Joins("LEFT JOIN car_cons ON car_cons.id = contracts.cid").
			Where("car_cons.id = ?", id).
			Debug().
			Find(&contracts)

		jsonData, _ := json.Marshal(contracts)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour)
	} else {
		json.Unmarshal([]byte(cacheData), &contracts)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": contracts,
	})
	return
}
